package sk.stuba.fiit.ltasys.web.mvc.model.entity;

import java.security.cert.CRLException;
import java.security.cert.X509CRL;
import java.util.Arrays;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * CRL Entity.
 * 
 * @author Marek Hruska
 */

@Entity
@Table(name = "CRL")
public class CrlEntity {

	public static final String ISSUER = "issuer";
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Integer crl_id = null;
	private Date updateTime = null;
	private Date downloadTime = null;
	private Date nextUpdate = null;
	private String issuer = null;
	private byte[] crl = null;

	
	public void convertFtomX09CRL(X509CRL crl2) throws CRLException {
		this.updateTime= crl2.getThisUpdate();
		this.downloadTime = new Date();
		this.issuer = crl2.getIssuerDN().toString();
		this.crl = crl2.getEncoded();
		this.nextUpdate = crl2.getNextUpdate();
	}
	
	public CrlEntity() {
	}

	

	public Integer getCrl_id() {
		return crl_id;
	}



	public void setCrl_id(Integer crl_id) {
		this.crl_id = crl_id;
	}



	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	public String getIssuer() {
		return issuer;
	}

	public void setIssuer(String issuer) {
		this.issuer = issuer;
	}

	public byte[] getCrl() {
		return crl;
	}

	public void setCrl(byte[] crl) {
		this.crl = crl;
	}



	public Date getDownloadTime() {
		return downloadTime;
	}

	public void setDownloadTime(Date downloadTime) {
		this.downloadTime = downloadTime;
	}

	public Date getNextUpdate() {
		return nextUpdate;
	}

	public void setNextUpdate(Date nextUpdate) {
		this.nextUpdate = nextUpdate;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + Arrays.hashCode(crl);
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		CrlEntity other = (CrlEntity) obj;
		if (!Arrays.equals(crl, other.crl))
			return false;
		return true;
	}



}
